//
//  FreeValuationUploadImgItemCell.swift
//  Kool Car
//
//  Created by huangweiqiang on 2022/4/20.
//

import UIKit

protocol FreeValuationUploadImgItemCellDelegate: NSObjectProtocol {
    ///删除照片事件
    func deleteImageAction(atIndex: NSInteger)
}
class FreeValuationUploadImgItemCell: UICollectionViewCell {
    var delegate: FreeValuationUploadImgItemCellDelegate!
    var bkgView: UIView!
    var imageViewBtn: UIButton!
    var deleteBtn: UIButton!
    var atIndex: NSInteger = 0
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = .white
        bkgView = UIView.init(frame: CGRect.zero)
        bkgView.backgroundColor = UIColor.white
        bkgView.layer.cornerRadius = 8
        bkgView.layer.masksToBounds = true
        self.contentView.addSubview(bkgView)
        
        self.imageViewBtn = UIButton.init(frame: CGRect.zero)
        self.imageViewBtn.setImage(UIImage.init(named: "icon_update"), for: .normal)
        self.imageViewBtn.setImage(UIImage.init(named: "icon_update"), for: .highlighted)
        self.imageViewBtn.isUserInteractionEnabled = false
        self.imageViewBtn.contentMode = .scaleAspectFill
        self.imageViewBtn.clipsToBounds = true
        bkgView.addSubview(self.imageViewBtn)
     
   
        deleteBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))
        deleteBtn.setImage(UIImage.init(named: "icon_close"), for: .normal)
        deleteBtn.setImage(UIImage.init(named: "icon_close"), for: .highlighted)
        deleteBtn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
        self.contentView.addSubview(deleteBtn)
        
        self.addSnpLayout()
    }
    
    func addSnpLayout() {
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(4)
            make.height.equalTo(75)
            
        }
      
        
        imageViewBtn.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(0)
        }
        
        deleteBtn.snp.makeConstraints { make in
            make.right.equalTo(3)
            make.top.equalTo(-3)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
        }
    }
    
 
    ///删除照片事件
    @objc func deleteBtnAction(){
        self.delegate?.deleteImageAction(atIndex: self.atIndex)
    }
    
    ///赋值
    func setUpLoadImgModel(itemModel: UpLoadImgModel) {
        self.imageViewBtn.setBackgroundImage(itemModel.image, for: .normal)
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

